# cmake version
cmake_minimum_required(VERSION 3.10)

set(CONFIGURE_DEPENDS OFF)
set(CMAKE_DEPENDS_IN_PROJECT_ONLY ON)
#set LIB_NAME
set(LIB_NAME "cc_sdk")

## include directories
find_package(Threads REQUIRED)

## add sources cpp c hpp h
file(GLOB_RECURSE USR_Module_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Module/*.cpp)
file(GLOB_RECURSE USR_Module_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/Module/*.h*)
list(APPEND USR_Module_ALL_FILES ${USR_Module_SOURCES} ${USR_Module_HEADER})



include_directories(${CMAKE_SOURCE_DIR}/src/Module)



set(LIB_SOURCES InterfaceSDK.cpp
#        main.cpp
        ${USR_Module_ALL_FILES}
)

add_library( ${LIB_NAME} SHARED  ${LIB_SOURCES} )

# message(WARNING "DEBUG [SRC]: COMPILER_SYSTEM = ${COMPILER_SYSTEM}")

if(${COMPILER_SYSTEM} STREQUAL "WINDOWS")
	list(FILTER USR_Module_ALL_FILES EXCLUDE REGEX ".*_linux\\..*")
else()
  # message(WARNING "DEBUG [ELSEIF_CHECK]: COMPILER_SYSTEM before strip = '${COMPILER_SYSTEM}'")
  string(STRIP "${COMPILER_SYSTEM}" CLEANED_COMPILER_SYSTEM)
  # message(WARNING "DEBUG [ELSEIF_CHECK]: COMPILER_SYSTEM after strip = '${CLEANED_COMPILER_SYSTEM}'")
  if(CLEANED_COMPILER_SYSTEM STREQUAL "LINUX")
    # message(WARNING "DEBUG [ELSEIF_CHECK]: Entering LINUX block.")
    list(FILTER USR_Module_ALL_FILES EXCLUDE REGEX ".*_win32\\..*")
          find_custom_library(LIB_NAME libunwind LIB_TYPE SHARED)
          # message(WARNING "DEBUG [SRC]: After find_custom_library for libunwind.")
          # message(WARNING "DEBUG [SRC]: LIBUNWIND_FOUND = ${LIBUNWIND_FOUND}")
          # message(WARNING "DEBUG [SRC]: LIBUNWIND_INCLUDE_DIRS = ${LIBUNWIND_INCLUDE_DIRS}")
          target_include_link_lib(${LIB_NAME} libunwind)
  else()
    # message(WARNING "DEBUG [ELSEIF_CHECK]: LINUX condition FAILED. Cleaned COMPILER_SYSTEM was '${CLEANED_COMPILER_SYSTEM}'.")
  endif()
endif()
target_link_libraries(${LIB_NAME} ${CMAKE_THREAD_LIBS_INIT})
# 指定动态库版本
# VERSION 动态库版本
# SOVERSION API版本
set_target_properties(${LIB_NAME}  PROPERTIES VERSION 1.0 SOVERSION 1)

install(TARGETS ${LIB_NAME}  ARCHIVE	 DESTINATION lib CONFIGURATIONS Release CONFIGURE_DEPENDS OFF)


# 获取所有 .h 和 .hpp 文件的列表
file(GLOB_RECURSE HEADER_FILES 
     "${CMAKE_SOURCE_DIR}/src/Module/*.h"
     "${CMAKE_SOURCE_DIR}/src/Module/*.hpp")


     install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/Module 
     DESTINATION ${CMAKE_SOURCE_DIR}/Release/include 
     FILES_MATCHING 
     PATTERN "*.h" 
     PATTERN "*.hpp"
     PATTERN "DefaultInternalModuleFactory.hpp" EXCLUDE
     PATTERN "InternalModuleFactory.hpp" EXCLUDE)

           


install(FILES ${CMAKE_SOURCE_DIR}/src/InterfaceSDK.h
        DESTINATION ${CMAKE_SOURCE_DIR}/Release/include)

add_custom_target(install_src
        ${CMAKE_COMMAND} --build . --target install
        COMMENT "Installing src")
    


# 在子 CMakeLists 文件中使用变量
if(${COMPILER_ARCH} STREQUAL "AARCH64")
add_custom_command(TARGET ${LIB_NAME} POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy
                           $<TARGET_FILE:${LIB_NAME}>
                           ${CMAKE_SOURCE_DIR}/Release/lib/aarch64-journey/)
elseif(${COMPILER_ARCH} STREQUAL "X86_64")
add_custom_command(TARGET ${LIB_NAME} POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy
                           $<TARGET_FILE:${LIB_NAME}>
                           ${CMAKE_SOURCE_DIR}/Release/lib/ubuntu/)
endif()                           